package com.jdk.demo.fileChannel.positionRead;

import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

// read(ByteBuffer dst, long position)方法的作用是将通道的指定位置的字节序列读入给定的缓冲区的当前位置
// 此方法不修改通道的position；如果给定的位置大于该文件的当前大小，则不读取任何字节
// 此方法具有同步性
public class Test8_1 {

	private static FileInputStream fisRef;
	private static FileChannel fileChannel;
	
	// 验证read方法从通道读取的字节放入缓冲区的remaining空间中
	public static void main(String[] args) throws Exception {
		fisRef = new FileInputStream(new File("h.txt"));
		fileChannel = fisRef.getChannel();
		
		ByteBuffer bytebuffer = ByteBuffer.allocate(100);
		bytebuffer.position(1);
		bytebuffer.limit(3);
		
		fileChannel.read(bytebuffer, 2);

		bytebuffer.rewind();
		for (int i = 0; i < bytebuffer.limit(); i++) {
			byte eachByte = bytebuffer.get();
			if(eachByte == 0){
				System.out.print(" 空格 ");
			}else{
				System.out.print((char)eachByte);
			}
		}
		
		fileChannel.close();
		fisRef.close();
	}
	
}
